package com.sunsys.mapreduce.examples.counters;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

public class AverageMapper extends Mapper<LongWritable, Text, NullWritable, IntWritable> {

	public AverageMapper() {
	}

	@Override
	protected void setup(Mapper<LongWritable, Text, NullWritable, IntWritable>.Context context) throws IOException, InterruptedException {
		/* initialize the User defined Dynamic counter */
		
	}
	
	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, NullWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		
		Counter counter = context.getCounter("com.sunsys.examples.dynamic.countergroup", "counter1");
		if(counter !=null) {
			counter.increment(1);
		} else {
			System.out.println("counter is null");
		}
		
		System.out.println("record");
	}
	
}
